package com.unhz.taigu.interface_controllers.order;

import com.unhz.generator.pojo.*;
import com.unhz.generator.utils.EmptyUtil;
import com.unhz.generator.utils.JsonResult;
import com.unhz.taigu.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import sun.dc.pr.PRError;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 沈佳庆
 * @Description:
 * @Date: Created in 下午 12:49 2018/3/5 0005
 * @Modified By: V1.0.0
 */
@RestController
@RequestMapping("/IOrder")
public class IOrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private TryOrderService tryOrderService;

    @Autowired
    private TryOrderInfoService tryOrderInfoService;

    @Autowired
    private InsoleService insoleService;

    @Autowired
    private ProductionOrderService productionOrderService;

    @Autowired
    private ProductionOrderInfoService productionOrderInfoService;

    @Autowired
    private DeliveryOrderService deliveryOrderService;

    @Autowired
    private DeliveryInfoService deliveryInfoService;

    @Autowired
    private ContractService contractService;
    @Autowired
    private AreaDictService areaDictService;

    /**
     * 订单页面接口
     * @param model
     * @param userId
     * @return
     */
    @RequestMapping("/selectOrder")
    public JsonResult selectOrder(Model model,@RequestParam(value= "userId",required = false) Integer userId){
        List<Order> orderList = orderService.selectOrder(userId);
        if(0==orderList.size()){
            return JsonResult.failure("查无订单!");
        }
        List<Order> list = new ArrayList<Order>();
        Order order = null;
        for(int i=0;i<orderList.size();i++){
            order = orderList.get(i);//订单表
            Integer orderId = orderList.get(i).getId();//订单ID
            Integer sampleId = orderList.get(i).getSampleId();//样品id
            //查询合同信息
            List<Contract> contractList = contractService.findContractsByOrderId(orderId);
            order.setContractList(contractList);
            //查询中底信息
            Insole insole = insoleService.selectByPrimaryKey(sampleId);
            order.setInsole(insole);
            //查询试做订单信息
            TryOrder tryOrder = tryOrderService.selectByOrderId(orderId);
            if(EmptyUtil.isEmpty(tryOrder)){
                list.add(order);
                continue;
            }
            Integer tryId = tryOrder.getId();
            List<TryOrderInfo> tryOrderInfoList = tryOrderInfoService.selectTryOrderInfoList(tryId);
            order.setTryOrder(tryOrder);
            tryOrder.setTryOrderInfoList(tryOrderInfoList);
            //排产订单信息
            List<ProductionOrder> productionOrderList = productionOrderService.selectByOrderId(Long.valueOf(orderId));
            List<ProductionOrder> productionOrders = new ArrayList<ProductionOrder>();
            for(int j=0;j<productionOrderList.size();j++){
                //排产订单
                ProductionOrder productionOrder = productionOrderList.get(j);
                productionOrders.add(productionOrder);
                Integer productionId = productionOrder.getId();
                List<ProductionOrderInfo> productionOrderInfoList = productionOrderInfoService.selectByOrderId(Long.valueOf(productionOrder.getId()));
                productionOrder.setProductionOrderInfoList(productionOrderInfoList);
                List<DeliveryOrder> deliveryOrderList = deliveryOrderService.selectDeliveryOrder(Long.valueOf(productionId));
                List<DeliveryOrder> deliveryOrders = new ArrayList<DeliveryOrder>();
              for(int x=0;x<deliveryOrderList.size();x++){
                    DeliveryOrder deliveryOrder = deliveryOrderList.get(x);
                  System.out.println("------------------"+deliveryOrder.getProductionId());
                    //查询订单数量
                    List<DeliveryInfo> deliveryInfoList = deliveryInfoService.selectByDeliveryOrderId(deliveryOrder.getId());
//                    productionOrder.setDeliveryOrder(deliveryOrder);
                      deliveryOrder.setDeliveryInfoList(deliveryInfoList);
                    deliveryOrders.add(deliveryOrder);
                }
                productionOrder.setDeliveryOrder(deliveryOrders);
            }
            order.setProductionOrder(productionOrders);
            list.add(order);
        }
        model.addAttribute("list",list);
        model.addAttribute("areaDict",areaDictService.selectAreaDictAll());
        return JsonResult.success("查询订单成功",model);
    }
}
